﻿using System;
using VegetableSale.NET.Dao;
using VegetableSale.NET.Entities;
using VegetableSale.NET.Entities.Good;

namespace VegetableSale.NET.Services;

public class RefundService
{
    private RefundMapper refundMapper = new RefundMapper();
    private GoodService goodService = new GoodService();
    private GoodMapper goodMapper = new GoodMapper();
    private StockService stockService = new StockService();
    private StockMapper stockMapper = new StockMapper();

    public Result getRefundList(QueryDTO queryDTO)
    {
        RefundPage refundPage = new RefundPage();
        uint pageNo = queryDTO.PageNo;
        uint pageSize = queryDTO.PageSize;
        List<Refund> refunds = refundMapper.getRefundList(queryDTO);
        List<RefundListReturn> returnRefunds = new List<RefundListReturn>();

        if (pageSize * pageNo - 1 >= refunds.Count)
        {
            for (uint i = pageSize * (pageNo - 1); i <= refunds.Count - 1; i++)
            {
                Refund refund = refunds[(int)i];
                string commname = goodMapper.getCommnameById(refund.Commid);
                Console.WriteLine("Commname:"+commname);
                RefundListReturn refundListReturn = new RefundListReturn(refund.Id, refund.Commid, commname, refund.Rfmoney,
                    refund.Rfnumber, refund.Remark, refund.Reftime, null, null);
                returnRefunds.Add(refundListReturn);
            }
        }
        else
        {
            for (uint i = pageSize * (pageNo - 1); i <= pageSize * pageNo - 1; i++)
            {
                Refund refund = refunds[(int)i];
                string commname = goodMapper.getCommnameById(refund.Commid);
                Console.WriteLine("Commname:"+commname);
                RefundListReturn refundListReturn = new RefundListReturn(refund.Id, refund.Commid, commname, refund.Rfmoney,
                    refund.Rfnumber, refund.Remark, refund.Reftime, null, null);
                returnRefunds.Add(refundListReturn);
            }
        }

        refundPage.Records = returnRefunds;
        refundPage.Pages = (int)pageNo;
        refundPage.Size = pageSize;
        refundPage.Total = (uint)refunds.Count;

        return new Result(200, "搜索成功", refundPage);
    }

    public Result addRefund(RefundAddQuery refundAddQuery)
    {
        // int newSaleMount = rtsaleQuery.Salemount - rtsaleQuery.Rtsnumber;
        // int newSaleMoney = newSaleMount * rtsaleQuery.Salemoney;
        // if (newSaleMount <= 0)
        // {
        //     newSaleMount = 0;
        //     saleService.deleteSale(rtsaleQuery.Id);
        // }
        //
        // saleMapper.updateSaleMount(rtsaleQuery.Id, newSaleMount, newSaleMoney);
        // rtsaleMapper.addRtsale(rtsaleQuery);
        int newStockNumber = refundAddQuery.Sknumber - refundAddQuery.Rfnumber;
        int newStockMoney = newStockNumber * refundAddQuery.Skmoney;
        if (newStockNumber<=0)
        {
            stockService.deleteStock(refundAddQuery.Id);
        }
        else
        {
            stockMapper.updateStockMount(refundAddQuery.Skid, newStockNumber, newStockMoney);
            goodService.updateInventory(refundAddQuery.Commid, refundAddQuery.Rfnumber, "-");
        }

        refundMapper.addRefund(refundAddQuery);
        return new Result(200, "退货成功", "");
    }
}

